package br.com.mocamorena.view.panel;

import br.com.mocamorena.model.Cliente;
import br.com.mocamorena.model.constant.EstadoCivil;
import br.com.mocamorena.model.constant.Sexo;
import br.com.mocamorena.view.UtilitiesGUI;
import java.util.Date;
import javax.swing.JLabel;

public final class PessoaFisicaPanel extends javax.swing.JPanel {

    public PessoaFisicaPanel() {
        initComponents();

        estadoCivilField.setModel(new javax.swing.DefaultComboBoxModel(EstadoCivil.values()));
        sexoField.setModel(new javax.swing.DefaultComboBoxModel(Sexo.values()));

        hideErrorsLabels();
    }

    public void hideErrorsLabels() {
        sexoErrorLabel.setVisible(false);
        nomeErrorLabel.setVisible(false);
        estadoCivilErrorLabel.setVisible(false);
        dataNascErrorLabel.setVisible(false);
        cpfErrorLabel.setVisible(false);
        identidadeErrorLabel.setVisible(false);
        orgaoExpErrorLabel.setVisible(false);
    }

    public void disableSexoField() {
        sexoField.setEnabled(false);
    }

    public void disableNomeField() {
        nomeField.setEnabled(false);
    }

    private Sexo getSexo() {
        return Sexo.valueOf(sexoField.getSelectedItem().toString());
    }

    private void setSexo(Sexo sexoPessoa) {
        sexoField.setSelectedIndex(sexoPessoa.ordinal());
    }

    public String getNome() {
        return nomeField.getText().trim().toUpperCase();
    }

    private void setNome(String nome) {
        nomeField.setText(nome);
    }

    private EstadoCivil getEstadoCivil() {
        return ((EstadoCivil) estadoCivilField.getSelectedItem());
    }

    private void setEstadoCivil(EstadoCivil estadoCivil) {
        if (estadoCivil == null) {
            return;
        }

        estadoCivilField.setSelectedIndex(estadoCivil.ordinal());
    }

    private Date getDataNasc() {
        return dataNascField.getDate();
    }

    private void setDataNasc(Date dataNasc) {
        dataNascField.setDate(dataNasc);
    }

    private String getCpf() {
        if(".   .   -".equals(cpfField.getText().trim())) {
            return null;
        }
        return cpfField.getText().trim();
    }

    private void setCpf(String cpf) {
        cpfField.setText(cpf);
    }

    private String getIdentidade() {
        return identidadeField.getText().trim();
    }

    private void setIdentidade(String identidade) {
        identidadeField.setText(identidade);
    }

    private String getOrgaoExp() {
        return orgaoExpField.getText().trim().toUpperCase();
    }

    private void setOrgaoExp(String orgaoExp) {
        orgaoExpField.setText(orgaoExp);
    }

    public JLabel getNomeErrorLabel() {
        return nomeErrorLabel;
    }

    public void fillEntity(Cliente pessoaFisica) {
        if (pessoaFisica == null) {
            return;
        }

        pessoaFisica.setSexo(getSexo());
        pessoaFisica.setNome(getNome());
        pessoaFisica.setEstadoCivil(getEstadoCivil());
        pessoaFisica.setDataNascimento(getDataNasc());
        pessoaFisica.setCadastroPessoa(getCpf());
        pessoaFisica.setRg(getIdentidade());
        pessoaFisica.setOrgExp(getOrgaoExp());

    }

    public void fillPanel(Cliente pessoaFisica) {
        if (pessoaFisica == null) {
            return;
        }

        setSexo(pessoaFisica.getSexo());
        setNome(pessoaFisica.getNome());
        setEstadoCivil(pessoaFisica.getEstadoCivil());
        setDataNasc(pessoaFisica.getDataNascimento());
        setCpf(pessoaFisica.getCadastroPessoa());
        setIdentidade(pessoaFisica.getRg());
        setOrgaoExp(pessoaFisica.getOrgExp());
    }

    @Override
    public boolean requestFocusInWindow() {
        super.requestFocusInWindow();

        if (!nomeField.isEnabled()) {
            return estadoCivilField.requestFocusInWindow();
        } else {
            return sexoField.requestFocusInWindow();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sexoLabel = new javax.swing.JLabel();
        sexoField = new javax.swing.JComboBox();
        nomeLabel = new javax.swing.JLabel();
        nomeField = new javax.swing.JTextField();
        estadoCivilLabel = new javax.swing.JLabel();
        estadoCivilField = new javax.swing.JComboBox();
        dataNascLabel = new javax.swing.JLabel();
        cpfLabel = new javax.swing.JLabel();
        cpfField = new javax.swing.JFormattedTextField();
        identidadeLabel = new javax.swing.JLabel();
        identidadeField = new javax.swing.JTextField();
        orgaoExpLabel = new javax.swing.JLabel();
        orgaoExpField = new javax.swing.JTextField();
        dataNascField = new org.jdesktop.swingx.JXDatePicker();
        sexoErrorLabel = new javax.swing.JLabel();
        estadoCivilErrorLabel = new javax.swing.JLabel();
        cpfErrorLabel = new javax.swing.JLabel();
        identidadeErrorLabel = new javax.swing.JLabel();
        orgaoExpErrorLabel = new javax.swing.JLabel();
        nomeErrorLabel = new javax.swing.JLabel();
        dataNascErrorLabel = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        sexoLabel.setText("Sexo:");
        add(sexoLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(53, 3, -1, -1));

        sexoField.setName(""); // NOI18N
        add(sexoField, new org.netbeans.lib.awtextra.AbsoluteConstraints(99, 0, 130, -1));
        UtilitiesGUI.respondToArrowEnterKey(sexoField);

        nomeLabel.setText("Nome:");
        add(nomeLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 29, -1, -1));
        add(nomeField, new org.netbeans.lib.awtextra.AbsoluteConstraints(99, 26, 383, -1));
        UtilitiesGUI.respondToArrowEnterKey(nomeField);

        estadoCivilLabel.setText("Estado Civil:");
        add(estadoCivilLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(22, 55, -1, -1));

        add(estadoCivilField, new org.netbeans.lib.awtextra.AbsoluteConstraints(99, 53, 130, -1));
        UtilitiesGUI.respondToArrowEnterKey(estadoCivilField);

        dataNascLabel.setText("Data de nasc.:");
        add(dataNascLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(289, 55, -1, -1));

        cpfLabel.setText("CPF:");
        add(cpfLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(58, 81, -1, -1));

        try {
            cpfField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        cpfField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        add(cpfField, new org.netbeans.lib.awtextra.AbsoluteConstraints(99, 78, 130, -1));
        UtilitiesGUI.respondToArrowEnterKey(cpfField);

        identidadeLabel.setText("Identidade:");
        add(identidadeLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(25, 104, -1, 20));
        add(identidadeField, new org.netbeans.lib.awtextra.AbsoluteConstraints(99, 104, 130, -1));
        UtilitiesGUI.respondToArrowEnterKey(identidadeField);

        orgaoExpLabel.setText("Org. Exp.:");
        add(orgaoExpLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 100, -1, 20));

        orgaoExpField.setAutoscrolls(false);
        add(orgaoExpField, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 100, 52, -1));
        UtilitiesGUI.respondToArrowEnterKey(orgaoExpField);
        add(dataNascField, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 52, 113, 20));
        UtilitiesGUI.respondToArrowEnterKey(dataNascField);

        sexoErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        sexoErrorLabel.setVerifyInputWhenFocusTarget(false);
        add(sexoErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(233, 0, -1, -1));

        estadoCivilErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        estadoCivilErrorLabel.setVerifyInputWhenFocusTarget(false);
        add(estadoCivilErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(233, 53, -1, -1));

        cpfErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        cpfErrorLabel.setVerifyInputWhenFocusTarget(false);
        add(cpfErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(233, 78, -1, -1));

        identidadeErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        identidadeErrorLabel.setVerifyInputWhenFocusTarget(false);
        add(identidadeErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(233, 104, -1, -1));

        orgaoExpErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        orgaoExpErrorLabel.setVerifyInputWhenFocusTarget(false);
        add(orgaoExpErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(486, 96, 20, 30));

        nomeErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        nomeErrorLabel.setVerifyInputWhenFocusTarget(false);
        add(nomeErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(486, 26, -1, -1));

        dataNascErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        dataNascErrorLabel.setVerifyInputWhenFocusTarget(false);
        add(dataNascErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(486, 52, -1, -1));
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JLabel cpfErrorLabel;
    private javax.swing.JFormattedTextField cpfField;
    private javax.swing.JLabel cpfLabel;
    protected javax.swing.JLabel dataNascErrorLabel;
    private org.jdesktop.swingx.JXDatePicker dataNascField;
    private javax.swing.JLabel dataNascLabel;
    protected javax.swing.JLabel estadoCivilErrorLabel;
    private javax.swing.JComboBox estadoCivilField;
    private javax.swing.JLabel estadoCivilLabel;
    protected javax.swing.JLabel identidadeErrorLabel;
    private javax.swing.JTextField identidadeField;
    private javax.swing.JLabel identidadeLabel;
    protected javax.swing.JLabel nomeErrorLabel;
    private javax.swing.JTextField nomeField;
    private javax.swing.JLabel nomeLabel;
    protected javax.swing.JLabel orgaoExpErrorLabel;
    private javax.swing.JTextField orgaoExpField;
    private javax.swing.JLabel orgaoExpLabel;
    protected javax.swing.JLabel sexoErrorLabel;
    private javax.swing.JComboBox sexoField;
    private javax.swing.JLabel sexoLabel;
    // End of variables declaration//GEN-END:variables
}